package com.tgy.nowcoder;

import java.util.HashMap;
import java.util.Map;

public class NC41_最长无重复子串 {

    /**
     *
     * @param arr int整型一维数组 the array
     * @return int整型
     */
    public static int maxLength (int[] arr) {
        // write code here

        if (arr.length == 1) {

            return 1;
        }

        int left = 0,right = 0;
        Map<Integer,Integer> map = new HashMap<>();
        int res = 0;
        while (right < arr.length) {

            int val = arr[right];
            Integer count = map.get(val);
            if (count == null || count == 0) {

                res = Math.max(res,right - left + 1);
                map.put(val,1);
                right++;
            }else {

                while (true) {

                    int leftVal = arr[left];
                    map.put(leftVal, map.get(leftVal) - 1);
                    left++;

                    if (left == right || leftVal == val) {
                        break;
                    }
                }
            }
        }

        return res;
    }

    public static void main(String[] args) {

        int[] nums = {2,2,1};
        System.out.println(maxLength(nums));
    }
}
